package com.winning.dps.rda.standardManage.service.impl;

import com.winning.dps.rda.standardManage.dao.LogDao;
import com.winning.dps.rda.standardManage.dao.UserDao;
import com.winning.dps.rda.standardManage.entity.Log;
import com.winning.dps.rda.standardManage.entity.User;
import com.winning.dps.rda.standardManage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LogDao logDao;

    @Override
    public List<User> findUserList() {
        return userDao.selectList(null);
    }

    @Override
    public List<Log> findLogList() {
        return logDao.selectList(null);
    }

    @Override
    public void multiQuery() {
        List<User> users = userDao.selectList(null);
        System.out.println("userList:" + users.toString());

        List<Log> logs = logDao.selectList(null);
        System.out.println("logList:" + logs.toString());
    }


    /**
     * 同一个事务中不能切换数据源，解决方案：拆分不同数据源的事务
     */
    @Transactional
    @Override
    public void methodTransErr() {
        List<User> users = userDao.selectList(null);
        System.out.println("userList:" + users.toString());

        List<Log> logs = logDao.selectList(null);
        System.out.println("logList:" + logs.toString());
    }

    /**
     * 同一个事务中不能切换数据源，解决方案：拆分不同数据源的事务
     */
    @Override
    public void methodTransOk() {
        method1Trans();
        method2Trans();
    }

    @Transactional
    @Override
    public void method1Trans() {
        List<User> users = userDao.selectList(null);
        System.out.println("userList:" + users.toString());
    }

    @Transactional
    @Override
    public void method2Trans() {
        List<Log> logs = logDao.selectList(null);
        System.out.println("logList:" + logs.toString());
    }

    @Override
    public List<User> findPageList(Map queryParams) {
        return userDao.findPageList(queryParams);
    }


}
